package com.xsir.ditan.admin.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author user
 * @date 2021/2/7
 */
@Slf4j
@RestController
@RequestMapping("api")
public class ApiController {
    @PostMapping("/upload")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) {
        String filename = file.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + "/src/main/resources/static/upload/";
        Map<String, Object> data = new HashMap<>(2);
        try {
            File dest = new File(filePath + filename);
            dest.createNewFile();
            file.transferTo(dest);
            data.put("code", 0);
            data.put("path", ("http://localhost:8080/static/upload/" + filename));
        } catch (IOException e) {
            data.put("code", -1);
            log.info(e.getMessage());
        }
        return data;
    }
}
